package com.gmlive.common.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

import com.gmlive.common.ui.R;
import com.gmlive.common.ui.dialog.detachable.DetachableOnShowListener;


/**
 * @Author: 姜志敏
 * @CreateDate: 2019-07-08 18:10
 */
public class IkAlertDialog extends BaseDialog implements DialogInterface {

    final AlertController mAlert;

    public IkAlertDialog(Context context) {
        this(context, 0);
    }

    public IkAlertDialog(Context context, int themeResId) {
        super(context, resolveDialogTheme(context, themeResId));
        mAlert = new AlertController(context, this, getWindow());
        initPrimaryStyle(mAlert);
    }

    protected IkAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        this(context, 0);
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAlert.installContent();
    }

    public Button getButton(int whichButton) {
        return mAlert.getButton(whichButton);
    }

    @Override
    public void setOnShowListener(@Nullable OnShowListener listener) {
        DetachableOnShowListener detachable = new DetachableOnShowListener(listener);
        detachable.clearOnDetach(this);
        super.setOnShowListener(detachable);
    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
    }

    @Override
    public void setOnCancelListener(@Nullable OnCancelListener listener) {
        super.setOnCancelListener(listener);
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        mAlert.setTitle(title);
    }

    public void setMessage(CharSequence message) {
        mAlert.setMessage(message);
    }

    public void setMessage(CharSequence message, boolean useFullWidth) {
        mAlert.setUseFullWidth(useFullWidth);
        mAlert.setMessage(message);
    }


    public void setView(View view) {
        mAlert.setView(view);
    }

    public void setView(int layoutResId) {
        mAlert.setView(layoutResId);
    }

    public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
        setButton(whichButton, text, true, listener);
    }

    public void setButton(int whichButton, CharSequence text, boolean autoDismiss, OnClickListener listener) {
        mAlert.setButton(whichButton, text, autoDismiss, listener, null);
    }

    public static int resolveDialogTheme(@NonNull Context context, @StyleRes int resid) {
        // Check to see if this resourceId has a valid package ID.
        if (((resid >>> 24) & 0x000000ff) >= 0x00000001) {   // start of real resource IDs.
            return resid;
        } else {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
            return outValue.resourceId;
        }
    }

    private void initPrimaryStyle(AlertController controller) {
        Context context = getContext();
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.ikDialogTheme, outValue, true);

        int titleColor = context.getResources().getColor(R.color.ik_alert_dialog_title);
        int messageColor = context.getResources().getColor(R.color.ik_alert_dialog_message);
        int messageNoTitleColor = context.getResources().getColor(R.color.ik_alert_dialog_message_no_title);
        int backgroundColor = context.getResources().getColor(R.color.ik_alert_dialog_background);
        int accentColor = context.getResources().getColor(R.color.ik_ui_theme_color);
        int dividerColor = context.getResources().getColor(R.color.ik_alert_dialog_divider);
        int buttonPressColor = context.getResources().getColor(R.color.ik_alert_dialog_button_press);
        int buttonTextColor = context.getResources().getColor(R.color.ik_alert_dialog_button_text);

        if (outValue.resourceId != Resources.ID_NULL) {
            TypedArray array = getContext().obtainStyledAttributes(outValue.resourceId, R.styleable.IkAlertDialogTheme);
            titleColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogTitleTextColor, titleColor);
            messageColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogMessageColor, messageColor);
            messageNoTitleColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogMessageNoTitleColor, messageNoTitleColor);
            backgroundColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogBackgroundColor, backgroundColor);
            accentColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogAccentColor, accentColor);
            dividerColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogDividerColor, dividerColor);
            buttonPressColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogButtonPressColor, buttonPressColor);
            buttonTextColor = array.getColor(R.styleable.IkAlertDialogTheme_ikDialogButtonTextColor, buttonTextColor);
            array.recycle();
        }

        controller.setStyleColors(accentColor, titleColor, messageColor, messageNoTitleColor,
                backgroundColor, dividerColor, buttonPressColor, buttonTextColor);
    }

    public static class Builder {
        private final AlertController.AlertParams P;
        private final int mTheme;

        public Builder(Context context) {
            this(context, resolveDialogTheme(context, 0));
        }

        public Builder(Context context, int themeResId) {
            P = new AlertController.AlertParams(context);
            mTheme = themeResId;
        }

        public Context getContext() {
            return P.mContext;
        }

        public Builder setTitle(CharSequence title) {
            P.mTitle = title;
            return this;
        }

        public Builder setTitle(int titleId) {
            P.mTitle = P.mContext.getText(titleId);
            return this;
        }

        public Builder setMessage(CharSequence message) {
            P.mMessage = message;
            return this;
        }

        public Builder setMessage(CharSequence message, boolean useFullWidth) {
            P.mMessage = message;
            P.useFullWidth = useFullWidth;
            return this;
        }

        public Builder setMessage(int messageId) {
            P.mMessage = P.mContext.getText(messageId);
            return this;
        }

        public Builder setMessage(int messageId, boolean useFullWidth) {
            P.mMessage = P.mContext.getText(messageId);
            P.useFullWidth = useFullWidth;
            return this;
        }

        public Builder setMessage(CharSequence message, int gravity) {
            P.mMessage = message;
            P.mGravity = gravity;
            return this;
        }

        public Builder setMessage(CharSequence message, int gravity, boolean useFullWidth) {
            P.mMessage = message;
            P.mGravity = gravity;
            P.useFullWidth = useFullWidth;
            return this;
        }

        public Builder setMessage(int resId, int gravity) {
            P.mMessage = P.mContext.getText(resId);
            P.mGravity = gravity;
            return this;
        }

        public Builder setMessage(int resId, int gravity, boolean useFullWidth) {
            P.mMessage = P.mContext.getText(resId);
            P.mGravity = gravity;
            P.useFullWidth = useFullWidth;
            return this;
        }

        public Builder setLink(CharSequence link, OnClickListener listener) {
            return setLink(link, true, listener);
        }

        public Builder setLink(int linkId, OnClickListener listener) {
            return setLink(linkId, true, listener);
        }

        public Builder setLink(CharSequence link, boolean autoDismiss, OnClickListener listener) {
            P.mLink = link;
            P.mLinkListener = listener;
            P.mLinkAutoDismiss = autoDismiss;
            return this;
        }

        public Builder setLink(int linkId, boolean autoDismiss, OnClickListener listener) {
            return setLink(P.mContext.getText(linkId), autoDismiss, listener);
        }

        public Builder setNegativeButton(CharSequence text, OnClickListener listener) {
            return setNegativeButton(text, true, listener);
        }

        public Builder setNegativeButton(int textId, OnClickListener listener) {
            return setNegativeButton(textId, true, listener);
        }

        public Builder setNegativeButton(int textId, boolean autoDismiss, OnClickListener listener) {
            return setNegativeButton(P.mContext.getText(textId), autoDismiss, listener);
        }

        public Builder setNegativeButton(CharSequence text, boolean autoDismiss, OnClickListener listener) {
            P.mNegativeButtonText = text;
            P.mNegativeButtonListener = listener;
            P.mNegativeAutoDismiss = autoDismiss;
            return this;
        }

        public Builder setPositiveButton(CharSequence text, OnClickListener listener) {
            return setPositiveButton(text, true, listener);
        }

        public Builder setPositiveButton(int textId, OnClickListener listener) {
            return setPositiveButton(textId, true, listener);
        }

        public Builder setPositiveButton(CharSequence text, boolean autoDismiss, OnClickListener listener) {
            P.mPositiveButtonText = text;
            P.mPositiveButtonListener = listener;
            P.mPositiveAutoDismiss = autoDismiss;
            return this;
        }

        public Builder setPositiveButton(int textId, boolean autoDismiss, OnClickListener listener) {
            return setPositiveButton(P.mContext.getText(textId), autoDismiss, listener);
        }

        public Builder setOnCancelListener(OnCancelListener cancelListener) {
            P.mOnCancelListener = cancelListener;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener dismissListener) {
            P.mOnDismissListener = dismissListener;
            return this;
        }

        public Builder setOnKeyListener(OnKeyListener keyListener) {
            P.mOnKeyListener = keyListener;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }

        public Builder setView(int layoutId) {
            P.mView = null;
            P.mViewLayoutResId = layoutId;
            return this;
        }

        public Builder setView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }

        public IkAlertDialog create() {
            final IkAlertDialog dialog = new IkAlertDialog(P.mContext, mTheme);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }

            return dialog;
        }

        public IkAlertDialog show() {
            final IkAlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}
